import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.util.Collection;

/**
 * Created by webrx on 2017/6/8.
 */@WebServlet("/text01") @MultipartConfig
public class Tupianshangchuan01 extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        Part part = req.getPart("file");
        Collection<String> coll = part.getHeaderNames();
        System.out.println("*********");
        for(String str:coll){

            System.out.println("part.getHeader("+str+")="+part.getHeader(str));
        }
        System.out.println("************");
        String upload = req.getServletContext().getRealPath("upload");
        File f = new File(upload);
        if(!f.exists()){
            f.mkdirs();
        }
        String name =part.getHeader("content-disposition");
        System.out.println(name+"++++++");
        String fn =name.substring(name.lastIndexOf("\\")+1,name.length()-1);
        System.out.println(fn);
        part.write(upload+"/"+fn);
        System.out.println("*******+++++++++");

       /* //jetty的方法上次图片
        String fnn =part.getSubmittedFileName();
        String fff =getServletContext().getRealPath("/upload/"+fn);
        FileOutputStream fos = new FileOutputStream(fff);
        InputStream is =part.getInputStream();
        byte temp[] = new byte[10240];
        int size=-1;
        while ((size=is.read(temp))!=-1){
            fos.write(temp,0,size);
        }
        fos.close();
        is.close();*/
    }
}
